import React, { memo } from 'react';
import { connect } from 'umi';
import { Typography } from 'antd';

import { sessionStorageUtil } from '@/pages/SalaryManage/utils/storageUtil';

const OrganizationName = ({ organizationCode, companySimpleList = [] }) => organizationCode
    ? companySimpleList?.find(item => item.organizationCode === organizationCode)?.organizationName || null
    : null

const OrganizationNameConnect = memo(
    connect(({ finance }) => ({ companySimpleList: finance.companySimpleList }))(OrganizationName),
);

const OrganizationNameHead = ({ organizationCode, style }) => <span style={{ fontSize: '14px', fontWeight: 400, ...style }}>
    <Typography.Text>
        <OrganizationNameConnect organizationCode={organizationCode} />
    </Typography.Text>
</span>

const getOrganizationName = organizationCode => [].concat(sessionStorageUtil.getItemInFinance('companySimpleList')).find(item => item.organizationCode === organizationCode)?.organizationName || null

export { OrganizationName, OrganizationNameConnect, OrganizationNameHead, getOrganizationName };
